package com.tanhua.server.controller;

import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementsService;
import com.tanhua.server.service.VideoMQService;
import com.tanhua.server.service.VideosService;
import com.tanhua.server.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("smallVideos")
public class VideosController {

    @Autowired
    private VideosService videosService;

    @Autowired
    private MovementsService movementsService;

    @Autowired
    private CommentsService commentsService;

    @Autowired
    private VideoMQService videoMQService;

    /**
     * 发布小视频
     *
     * @param picFile   图片封面
     * @param videoFile 小视频
     * @return 成功返回200，失败返回500
     */
    @PostMapping
    public ResponseEntity<Void> saveVideo(
            @RequestParam(value = "videoThumbnail", required = false) MultipartFile picFile,
            @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) {
        try {
            String videoId = videosService.saveVideo(picFile, videoFile);
            if (StringUtils.isNotEmpty(videoId)) {
                videoMQService.videoMsg(videoId);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询小视频列表
     *
     * @param page     页数
     * @param pageSize 每页显示的数量
     * @return 响应对象：状态码以及数据
     */
    @GetMapping
    public ResponseEntity<PageResult> queryVideoList(
            @RequestParam(value = "page", defaultValue = "2") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize
    ) {
        try {
            PageResult pageResult = videosService.queryVideoList(page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 小视频点赞
     *
     * @param videoId 小视频id
     * @return 响应对象：状态码以及数据
     */
    @PostMapping("/{id}/like")
    public ResponseEntity<Long> saveLikeVideo(@PathVariable("id") String videoId) {
        try {
            Long likeCount = movementsService.saveLikeComment(videoId);
            if (likeCount != null) {
                videoMQService.likeVideoMsg(videoId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 小视频取消点赞
     *
     * @param videoId 小视频id
     * @return 响应对象：状态码以及数据
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity<Long> disLikeComment(@PathVariable("id") String videoId) {
        try {

            Long likeCount = movementsService.removeLikeComment(videoId);
            if (likeCount != null) {
                videoMQService.disLikeVideoMsg(videoId);
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 发表评论
     *
     * @param videoId 视频id
     * @param param   参数：评论
     * @return 状态码：成功200，失败500
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity<Void> publishComment(@PathVariable("id") String videoId, @RequestBody Map<String, String> param) {
        String comment = param.get("comment");
        Boolean result = commentsService.saveComments(videoId, comment);
        if (result) {
            videoMQService.commentVideoMsg(videoId);
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 小视频评论点赞
     *
     * @param commentId 评论id
     * @return 响应对象：状态码以及点赞数量
     */
    @PostMapping("comments/{id}/like")
    public ResponseEntity<Long> saveLikeVideoComment(@PathVariable("id") String commentId) {
        try {
            Long likeCount = movementsService.saveLikeComment(commentId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 小视频评论取消点赞
     *
     * @param commentId 评论id
     * @return 响应对象：状态码以及点赞数量
     */
    @PostMapping("comments/{id}/dislike")
    public ResponseEntity<Long> disLikeVideoComment(@PathVariable("id") String commentId) {
        try {
            Long likeCount = movementsService.removeLikeComment(commentId);
            if (likeCount != null) {
                return ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 查询小视频评论列表
     *
     * @param videoId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity<PageResult> queryVideoList(
            @PathVariable("id") String videoId,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        try {
            PageResult pageResult = commentsService.queryCommentsList(videoId, page, pageSize);
            if (pageResult != null) {
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 关注用户
     *
     * @param userId 关注的用户id
     * @return 状态码：200 || 500
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity<Void> userFocus(@PathVariable("uid") Integer userId) {
        try {
            boolean result = videosService.userFocus(userId);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 取消关注用户
     *
     * @param userId 关注的用户id
     * @return 状态码：200 || 500
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity<Void> userUnFocus(@PathVariable("uid") Integer userId) {
        try {
            boolean result = videosService.userUnFocus(userId);
            if (result) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
